﻿using IMES.Common.Domain.DariusModel;
using IMES.Common.Dtos;
using IMES.Common.Infrastructure;
using MediatR;

namespace Darius.API.Application.Commands.Approval
{
    public class ApprovalUpdateHandler : IRequestHandler<ApprovalUpdateCommand, Result>
    {
        private readonly IBaseRepositorys<ApprovalApplications> approvalsrepository;

        public ApprovalUpdateHandler(IBaseRepositorys<ApprovalApplications> approvalsrepository)
        {
            this.approvalsrepository = approvalsrepository;
        }

        public async Task<Result> Handle(ApprovalUpdateCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CodeEnum.search };
            var res = approvalsrepository.GetAll().FirstOrDefault();
            if (res != null)
            {
                res.ApprovalStatus = request.Status;
            }
            result.Code = approvalsrepository.Update(res);
            if (request.Status == 1)
            {
                result.Messege = "审批通过";
            }
            else
            {
                result.Messege = "审批不通过";
            }
            return result;
        }
    }
}
